import time
from werobot import WeRoBot
from Applications.WeChat.ClassInfo import Dict180851
from utils.Robot.Robot import OwnThink
from utils.Robot.config import Access_Token, AppID, AppSecret, EncodingAESKey

werobot = WeRoBot(token=Access_Token,
                  encoding_aes_key = EncodingAESKey,
                  APP_ID=AppID,
                  APP_SECRET=AppSecret,
                  enable_session=True,)


@werobot.image
def bigLearn(message, session):
    # 用户名
    sender = message.source
    # 判断用户是否新用户
    try:
        # session[sender] = {"reg_time": message.time, "Class": "None", "ClassStatus": "0", "bigLearningStatus": "0"}
        user = session.get(sender)
    except:
        # 初始化
        session[sender] = {"reg_time": message.time, "Class": "None", "ClassStatus": "0", "bigLearningStatus": "0"}
        user = session.get(sender)
    user = session.get(sender)
    classmate = user.get("Class")
    name = Dict180851.get(classmate)
    if user.get("bigLearningStatus") == "1":
        changeInfo(sender, session, "bigLearningStatus", "0")
        return "/*青年大学习*/\n收到，" + name + ":\n" + message.img
    else:
        return "收到一张图片"


@werobot.text
def main(message, session):
    # TODO------------------初始化------------------
    # 用户名
    sender = message.source
    classmate = ""
    reg_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.time))
    init = {"reg_time": reg_time, "Class": "None", "ClassStatus": "0", "bigLearningStatus": "0", "ClassModel":0}
    # 判断用户是否新用户
    try:
        # session[sender] = init
        user = session.get(sender)
        classmate = user.get("Class")
    except:
        # 初始化
        session[sender] = init
        user = session.get(sender)

    if user.get("ClassModel") == "1":
        return "/*班级模式*/\n" + ClassModel(message, session, sender, user)
    else:
        return CommonModel(message, session, sender, user)


def CommonModel(message, session, sender, user):
    # TODO------------------判断命令------------------
    if message.content == "help" or message.content == "帮助":
        return 'help --帮助\ninfo --个人信息\nclass --班级模式'
    if message.content == "class" or message.content == "班级模式":
        changeInfo(sender, session, "ClassModel", "1")
        if user.get("Class") == "None":
            changeInfo(sender, session, "ClassStatus", "1")
            return "已切换到班级模式\n"+'help --帮助\n（*注意*）\n初次使用请输入学号：'
        return "已切换到班级模式\n命令：\n"+'help --帮助\ninfo --个人信息\n认证 --验证信息\n大学习 --发送截图\nexit --普通模式'
    if message.content == "info" or message.content == "个人信息":
        return info(user)

    # TODO------------------判断状态------------------
    if user.get("ClassStatus") == "1":
        ClassDict = Dict180851

    # TODO------------------状态清零------------------
    return OwnThink(message.content)


def ClassModel(message, session, sender, user):

    help = 'help --帮助\ninfo --个人信息\n认证 --验证信息\n大学习 --发送截图\nexit --普通模式'

    # TODO------------------判断命令------------------

    if message.content == "认证":
        changeInfo(sender, session, "ClassStatus", "1")
        return "180851班认证\n使用请输入：学号"

    if message.content == "info" or message.content == "个人信息":
        return info(user)

    if message.content == "exit" or message.content == "普通模式" or message.content == "退出":
        changeInfo(sender, session, "ClassModel", "0")
        return "已切换到普通模式"

    if message.content == "help" or message.content == "帮助":
        return help

    if message.content == "大学习":
        print(user)
        if user.get("Class") == "None":
            changeInfo(sender, session, "ClassStatus", "1")
            return "初次使用请输入：学号"
        else:
            changeInfo(sender, session,"bigLearningStatus","1")
            classmate = user.get("Class")
            name = Dict180851.get(classmate)
            return "%s请发送青年大学习图片" %name

    # TODO------------------判断状态------------------
    if user.get("ClassStatus") == "1":
        ClassDict = Dict180851
        try:
            ClassDict.get(message.content)
            name = ClassDict[message.content]
            changeInfo(sender, session, "Class", message.content)
            changeInfo(sender, session, "ClassStatus", "0")
            changeInfo(sender, session, "bigLearningStatus", "1")
            return "欢迎，%s" %name
        except:
            changeInfo(sender, session, "ClassStatus", "0")
            return '查询不到任何学号信息\n请再次输入"认证"\n(若多次失败请联系管理员)'

    # TODO------------------状态清零------------------
    changeInfo(sender, session, "bigLearningStatus", "0")
    return "未能识别\n\n" + 'help --帮助\ninfo --个人信息\n认证 --验证信息\n大学习 --发送截图\nexit --普通模式'

def changeInfo(userid, session, item, to):
    user = session.get(userid)
    user[item] = to

def info(items):
    classmate = items.get("Class")
    info = str(classmate) + "\n"
    for item in items:
        info += str(item) +":" + str(items[item]) + "\n"
    return info